﻿#include "HomePage.h"
#include "ui_HomePage.h"

#include <QDateTime>


HomePage::HomePage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HomePage)
{
    ui->setupUi(this);

    m_recoder = new Recorder;
    m_faceWgt = new FaceWgt;
    m_fingerWgt = new FingerWgt;

    connect(m_recoder, &Recorder::sigShowMsg, this, &HomePage::onPlayViocePrompt);
    connect(m_recoder, &Recorder::sigLoginFace, this, [this](){
        m_faceWgt->switchAutoVirtfy(false);
    }, Qt::QueuedConnection);
    connect(m_recoder, &Recorder::sigLoginFinger, m_fingerWgt, &FingerWgt::onUserLogin);
    connect(m_recoder, &Recorder::sigSearchModel, m_fingerWgt, &FingerWgt::onUserSearch);

    connect(m_faceWgt, &FaceWgt::sigUserLoginOver, m_recoder, &Recorder::onModuleLoginOver);
    connect(m_faceWgt, &FaceWgt::sigUserSign, m_recoder, &Recorder::onUserSign); //用户签到.
    connect(m_recoder, &Recorder::sigRemoveFace, m_faceWgt, &FaceWgt::onRemoveFace);
	connect(m_fingerWgt, &FingerWgt::sigUserLoginOver, m_recoder, &Recorder::onModuleLoginOver);
    connect(m_fingerWgt, &FingerWgt::sigUserSign, m_recoder, &Recorder::onUserSign); //用户签到.
    connect(m_recoder, &Recorder::sigRemoveFinger, m_fingerWgt, &FingerWgt::onRemoveFinger);

    connect(m_faceWgt, &FaceWgt::sigUpdateImage, this, &HomePage::onUpdateImage, Qt::QueuedConnection);
    connect(m_faceWgt, &FaceWgt::sigPlayViocePrompt, this, &HomePage::onPlayViocePrompt);
    connect(m_fingerWgt, &FingerWgt::sigPlayViocePrompt, this, &HomePage::onPlayViocePrompt);

    //菜单功能.
    connect(ui->homeBtn, &QPushButton::clicked, this, [this](){
        ui->stackedWidget->setCurrentIndex(0);
    });
    connect(ui->toolBtn, &QPushButton::clicked, this, [this](){
        ui->stackedWidget->setCurrentIndex(1);
    });
    connect(ui->settingBtn, &QPushButton::clicked, this, [this](){
        ui->stackedWidget->setCurrentIndex(2);
    });

    //主页.
    m_clockTimer = new QTimer(this);
    connect(m_clockTimer, &QTimer::timeout, this, [this](){
        QDateTime dateTime = QDateTime::currentDateTime();
        QString timeStr = dateTime.time().toString("hh:mm:ss"); //时间.
        QString dateStr = dateTime.date().toString(QString::fromLocal8Bit("MM月dd日 ")); //日期.
        ui->timeLab->setText(timeStr);
        switch(dateTime.date().dayOfWeek()){
        case 1: ui->dateLab->setText(dateStr.append(QString::fromLocal8Bit("周一"))); break;
        case 2: ui->dateLab->setText(dateStr.append(QString::fromLocal8Bit("周二"))); break;
        case 3: ui->dateLab->setText(dateStr.append(QString::fromLocal8Bit("周三"))); break;
        case 4: ui->dateLab->setText(dateStr.append(QString::fromLocal8Bit("周四"))); break;
        case 5: ui->dateLab->setText(dateStr.append(QString::fromLocal8Bit("周五"))); break;
        case 6: ui->dateLab->setText(dateStr.append(QString::fromLocal8Bit("周六"))); break;
        case 7: ui->dateLab->setText(dateStr.append(QString::fromLocal8Bit("周日"))); break;
        }
    });
    m_clockTimer->start(1000);

    //工具箱.
    connect(ui->employeeBtn, &QPushButton::clicked, this, [this](){
        m_recoder->setShowIndex(1);
        m_recoder->show();
        m_recoder->raise();
    });
    connect(ui->attendanceBtn, &QPushButton::clicked, this, [this](){
        m_recoder->setShowIndex(2);
        m_recoder->show();
        m_recoder->raise();
    });
    connect(ui->fingerDevBtn, &QPushButton::clicked, this, [this](){
        m_fingerWgt->show();
        m_fingerWgt->raise();
    });
    connect(ui->faceDevBtn, &QPushButton::clicked, this, [this](){
        m_faceWgt->show();
        m_faceWgt->raise();
    });

    //系统设置.
    connect(ui->applySetBtn, &QPushButton::clicked, this, &HomePage::onApplySetting);
    connect(ui->cancelSetBtn, &QPushButton::clicked, this, &HomePage::onCancelSetting);
    connect(ui->volumeSlider, &QSlider::valueChanged, this, [this](int value){
        ui->volumeLab->setText(QString("%1").arg(value/100.0, 0, 'f', 1));
    });
    connect(ui->rateSlider, &QSlider::valueChanged, this, [this](int value){
        ui->rateLab->setText(QString("%1").arg(value/100.0, 0, 'f', 1));
    });
    connect(ui->pitchSlider, &QSlider::valueChanged, this, [this](int value){
        ui->pitchLab->setText(QString("%1").arg(value/100.0, 0, 'f', 1));
    });

    m_tts.setLocale(QLocale::Chinese);
    //    QString textToRead = QString::fromLocal8Bit("欢迎使用智能打卡机");
    //    onPlayViocePrompt(textToRead);

    ui->homeBtn->setIcon(QIcon(":/icon/home.png"));
    ui->toolBtn->setIcon(QIcon(":/icon/toolkit.png"));
    ui->settingBtn->setIcon(QIcon(":/icon/setting.png"));

    ui->employeeBtn->setIcon(QIcon(":/icon/archives.png"));
    ui->attendanceBtn->setIcon(QIcon(":/icon/chart-table.png"));
    ui->fingerDevBtn->setIcon(QIcon(":/icon/finger.png"));
    ui->faceDevBtn->setIcon(QIcon(":/icon/face.png"));

    ui->employeeBtn->setIconSize(QSize(32, 32));
    ui->attendanceBtn->setIconSize(QSize(32, 32));
    ui->fingerDevBtn->setIconSize(QSize(32, 32));
    ui->faceDevBtn->setIconSize(QSize(32, 32));

    ui->homeBtn->setStyleSheet("QPushButton {border: none;"
                               "    background-color: transparent; /* 设置背景透明 */"
                               "    padding: 2px; /* 去除内边距 */}");
    ui->toolBtn->setStyleSheet("QPushButton {border: none;"
                               "    background-color: transparent; /* 设置背景透明 */"
                               "    padding: 2px; /* 去除内边距 */}");
    ui->settingBtn->setStyleSheet("QPushButton {border: none;"
                                  "    background-color: transparent; /* 设置背景透明 */"
                                  "    padding: 2px; /* 去除内边距 */}");

    ui->employeeBtn->setStyleSheet("QPushButton {border: none;"
                                   "    background-color: transparent; /* 设置背景透明 */"
                                   "    padding: 2px; /* 去除内边距 */}");
    ui->attendanceBtn->setStyleSheet("QPushButton {border: none;"
                                     "    background-color: transparent; /* 设置背景透明 */"
                                     "    padding: 2px; /* 去除内边距 */}");
    ui->fingerDevBtn->setStyleSheet("QPushButton {border: none;"
                                    "    background-color: transparent; /* 设置背景透明 */"
                                    "    padding: 2px; /* 去除内边距 */}");
    ui->faceDevBtn->setStyleSheet("QPushButton {border: none;"
                                  "    background-color: transparent; /* 设置背景透明 */"
                                  "    padding: 2px; /* 去除内边距 */}");
    return;
}

HomePage::~HomePage()
{
    if(m_faceWgt){
        m_faceWgt->close();
        m_faceWgt->forceStop();
        m_faceWgt->deleteLater();
    }
    if(m_fingerWgt){
        m_fingerWgt->close();
        m_fingerWgt->forceStop();
        m_fingerWgt->deleteLater();
    }
    delete ui;
}

void HomePage::closeEvent(QCloseEvent *event)
{
    m_faceWgt->close();
    m_fingerWgt->close();
    QWidget::closeEvent(event);
    return;
}

void HomePage::onPlayViocePrompt(QString text)
{
    if(m_speechFlag){
        // QVoice voice = QVoice::Teenager;
        m_tts.say(text); //这是一个异步接口.
    }
    return;
}

void HomePage::onApplySetting()
{
    m_companyName = ui->companyNameEdit->text();

    m_signInTime = ui->signInTimeEdit->time();
    m_signOutTime = ui->signOutTimeEdit->time();

    m_speechFlag = ui->speechOpenBox->isChecked() ? true : false;
    m_gender = ui->voiceBox->currentText();
    m_pitch = ui->pitchSlider->value() / 100.0;
    m_rate = ui->rateSlider->value() / 100.0;
    m_volume = ui->volumeSlider->value() / 100.0;

    ui->companyNameLab->setText(m_companyName);
    //m_tts.setLocale(const QLocale &locale);
    QVector<QVoice> voices = m_tts.availableVoices();
    foreach (const QVoice &voice, voices) { //遍历查找具有特定属性的声音.
        if (voice.name().contains(m_gender, Qt::CaseInsensitive)) {
            m_tts.setVoice(voice);
            break;
        }
    }
    m_tts.setPitch(m_pitch);
    m_tts.setRate(m_rate);
    m_tts.setVolume(m_volume);
    return;
}

void HomePage::onCancelSetting()
{
    ui->companyNameEdit->setText(m_companyName);

    ui->signInTimeEdit->setTime(m_signInTime);
    ui->signOutTimeEdit->setTime(m_signOutTime);

    ui->speechOpenBox->setChecked(m_speechFlag);
    ui->speechCloseBox->setChecked(!m_speechFlag);
    if(m_gender.contains("Male", Qt::CaseInsensitive)){
        ui->voiceBox->setCurrentIndex(0);
    }
    else {
        ui->voiceBox->setCurrentIndex(1);
    }
    ui->pitchSlider->setValue(qRound(m_pitch*100.0));
    ui->rateSlider->setValue(qRound(m_rate*100.0));
    ui->volumeSlider->setValue(qRound(m_volume*100.0));

    return;
}

void HomePage::onUpdateImage()
{
    m_pix = m_faceWgt->getCurImage();
    ui->faceLab->setPixmap(m_pix);
    return;
}


